/*
 * Copyright (C) 2012 BARBOTIN Nicolas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "GCColorButton.h"
#include <ctime>
#include <cstdlib>

GCColorButton::GCColorButton(QWidget *parent) :
    QPushButton(parent)
{
    srand(time(0));
    pickRandomColor();

    connect(this, SIGNAL(clicked()), this, SLOT(pickColor()));
}

QColor GCColorButton::getColor()
{
    return cColor;
}

void GCColorButton::paintEvent(QPaintEvent *pe)
{
    QPushButton::paintEvent(pe);

    QPainter p(this);
    QBrush b(cColor);

    p.setBrush(b);
    p.setPen(Qt::NoPen);
    p.drawRect(10, 10, width() - 20, height() - 20);
}

void GCColorButton::pickRandomColor(bool emitSignals)
{
    cColor.setRed(rand() % 256);
    cColor.setGreen(rand() % 256);
    cColor.setBlue(rand() % 256);
    cColor.setAlpha(255);

    update();
    if(emitSignals) {
        emit colorPicked(cColor);
        emit colorChanged();
    }
}

void GCColorButton::pickColor()
{
    cColor = QColorDialog::getColor(cColor, this, tr("Choisissez une couleur"), QColorDialog::ShowAlphaChannel);
    update();

    emit colorPicked(cColor);
    emit colorChanged();
}

void GCColorButton::setColor(QColor c, bool emitSignals)
{
    cColor = c;
    update();

    if(emitSignals) {
        emit colorPicked(cColor);
        emit colorChanged();
    }
}
